#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void unlock_acc(void)
{
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("acc path:%s\n",path);
	int fd = open(path,O_RDWR|O_EXCL,0644);
	if(ERROR == fd)
	{
		perror("open acc");
		strcpy(stoc.str,"要解锁的卡号不存在！\n");
	}
	else
	{
		Account aco;
		read(fd,&aco,sizeof(aco));
		aco.pass = ctos.acc.pass;
		aco.errno = 0;
		lseek(fd,0,SEEK_SET);
		write(fd,&aco,sizeof(aco));
		strcpy(stoc.str,"账户解锁成功！\n");
	}
	stoc_send(&stoc);//返回消息
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_UNLOCK);
		stoc.type = ctos.pid;//反馈消息类型为进程号
		unlock_acc();
	}
}
